/*
 * Copyright 2015 Hongxin Telecommunication Technologies Co, Ltd.,
 * Wuhan, Hubei, China. All rights reserved.
 */
package com.hxrd.util;

/**
 *
 * @author chenwei Created on 2016-12-22, 19:32:35
 */
import java.text.MessageFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Util
{

	public static boolean isboolIP(String ipAddress)
	{
		String ip
				= "(2[5][0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})";
		Pattern pattern = Pattern.compile(ip);
		Matcher matcher = pattern.matcher(ipAddress);
		return matcher.matches();
	}

	public static boolean isboolMAC(String macAddress)
	{
		String mac
				= "^[a-fA-F0-9]{2}+:[a-fA-F0-9]{2}+:[a-fA-F0-9]{2}+:[a-fA-F0-9]{2}+:[a-fA-F0-9]{2}+:[a-fA-F0-9]{2}$";
		Pattern pattern = Pattern.compile(mac);
		Matcher matcher = pattern.matcher(macAddress);
		return matcher.matches();
	}

	public static int gcd(int a, int b)
	{
		if(a < b)
		{
			// arrange so that a>b
			int temp = a;
			a = b;
			b = temp;
		}
		if(0 == b)// the base case
		{
			return a;
		}
		if(a % 2 == 0 && b % 2 == 0)// a and b are even
		{
			return 2 * gcd(a / 2, b / 2);
		}
		if(a % 2 == 0)// only a is even
		{
			return gcd(a / 2, b);
		}
		if(b % 2 == 0)// only b is even
		{
			return gcd(a, b / 2);
		}
		return gcd((a - b) / 2, b);// a and b are odd
	}

	public static String getString(String key, Object... params)
	{
		if(key == null || key.trim().equals(""))
		{
			return "";
		}

		MessageFormat mf = new MessageFormat(key);
		String result = mf.format(params);

		return result.trim();
	}

	public static void main(String[] args)
	{
		int v = gcd(2313, 123123);
		boolean match1 = isboolMAC("ab:22:cd:32:02:ef");
		boolean match2 = isboolMAC("a0-22:cd:32:02:ea");
		System.out.println("v is:" + v);
	}

}
